import { WindowRefService } from './../../services/utils/Window.ref.Service';
import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';

@Component({
      selector: 'app-paging',
      templateUrl: './paging.component.html',
      styleUrls: ['./paging.component.scss']
})
export class PagingComponent implements OnInit {

      @Input() totalRows: any;
      @Input() reqData: any;

      //页码
      public pages: Array<any>;
      public pagesC: Array<any>;
      public activeIndex: number = 0;

      //table 基本信息
      public startItem: any;
      public endItem: any
      public previouDisable: any;
      public nextDisable: any;
      public hideFoot:boolean = false;

      constructor(
            private _window: WindowRefService
      ) { }

      ngOnInit() {

            //初始化分页
            this.pageInfo();
            this.isDisablePreviou();
            this.isDisableNext();
            this.pageIndx();
            this.pageview();

            if(this.totalRows == 0){
                  this.hideFoot = true;
            }

      }

      @Output() loadData = new EventEmitter();

      //分页分页展示优化
      pageview() {
            let maxPage = this.pages.length;

            if (maxPage > 8) {
                  // console.log(this.pages.length);
                  this.pages = [1, 2, 3, 4, 5, '...', this.pages[maxPage - 1]];
            };

      }

      //查询
      tableRqu(index, totalRows) {

            this.activeIndex = index;
            this.totalRows = totalRows;

            this.pageInfo();
            this.isDisablePreviou();
            this.isDisableNext();
            this.pageIndx();
            this.pageview();
            
            if(this.totalRows == 0){
                  this.hideFoot = true;
            }else{
                  this.hideFoot = false;
            }
      }
      //页码
      pageIndx() {
            this.pages = [];
            this.pagesC = []
            let pageSize = this.reqData.PageSize;
            // pageSize为0   不执行循环 （分页不显示） 
            if (pageSize) {
                  for (let i = 0; i < this.totalRows / pageSize; i++) {
                        this.pages.push(i + 1);
                        this.pagesC.push(i + 1);
                  }
            }
      }

      //分页请求
      pageReq(index) {

            let activeIndex = index - 1;
            let pageIndex = activeIndex + 1;

            if (index != '...') {
                  this.pageUI(index, activeIndex, pageIndex)
            }

      }

      pageUI(index, activeIndex, pageIndex, pageGo?) {

            let maxPage = this.pages.length;
            let pagesC = this.pagesC.length;

            if (pagesC > 8) {

                  if (pageGo == "next") {

                        if (maxPage >= 7 && index >= 5 && index < pagesC - 3) {
                              this.pages = [1, '...', index, index + 1, index + 2, '...', this.pages[maxPage - 1]];
                              activeIndex = 3
                        }

                        //前五页
                        if (index < 5) {
                              this.pages = [1, 2, 3, 4, 5, '...', this.pages[maxPage - 1]];
                        }

                        //后五页开始
                        if (index == pagesC - 3) {
                              this.pages = [1, '...', index - 1, index, index + 1, index + 2, index + 3];
                              activeIndex = 4
                        }

                        if (index == pagesC - 2) {
                              activeIndex = 5
                        }

                        if (index == pagesC - 1) {
                              activeIndex = 6
                        }

                  } else if (pageGo == "previou") {

                        if (maxPage >= 7 && index >= 5 && index < this.pagesC.length - 2) {
                              this.pages = [1, '...', index - 1, index, index + 1, '...', this.pages[maxPage - 1]];
                              activeIndex = 3
                        }

                        if (index < 5) {
                              this.pages = [1, 2, 3, 4, 5, '...', this.pages[maxPage - 1]];
                        }

                        if (index == this.pagesC.length - 2) {
                              this.pages = [1, '...', index - 2, index - 1, index, index + 1, index + 2];
                        }

                        if (index == this.pagesC.length - 2) {
                              activeIndex = 4
                        }
                        if (index == this.pagesC.length - 1) {
                              activeIndex = 5
                        }

                        if (index == this.pagesC.length) {
                              activeIndex = 6
                        }

                  } else {

                        if (maxPage >= 7 && index >= 5 && index < this.pagesC.length - 2) {

                              this.pages = [1, '...', index - 1, index, index + 1, '...', this.pages[maxPage - 1]];
                              activeIndex = 3

                        } else if (index < 5) {
                              this.pages = [1, 2, 3, 4, 5, '...', this.pages[maxPage - 1]];

                        } else if (index == this.pagesC.length - 2) {
                              this.pages = [1, '...', index - 2, index - 1, index, index + 1, index + 2];

                        }

                        if (index == this.pagesC.length - 2) {
                              activeIndex = 4
                        }
                        if (index == this.pagesC.length - 1) {
                              activeIndex = 5
                        }

                        if (index == this.pagesC.length) {
                              this.pages = [1, '...', index - 4, index - 3, index - 2, index - 1, index];
                              activeIndex = 6
                        }

                  }

            }

            this.reqTable(activeIndex, pageIndex);
      }

      //下一页
      next() {

            let index: any = parseInt(this._window.nativeWindow.document.getElementsByClassName("pageActive")[0].innerText);

            let activeIndex = index;
            let pageIndex = index + 1;

            if (index != '...') {
                  this.pageUI(index, activeIndex, pageIndex, "next")
            }

      }
      //上一页
      previou() {

            let index: any = parseInt(this._window.nativeWindow.document.getElementsByClassName("pageActive")[0].innerText);

            let activeIndex = index - 2;
            let pageIndex = index - 1;
            if (index != '...') {
                  this.pageUI(index - 1, activeIndex, pageIndex, "previou")
            }
      }

      //请求
      private reqTable(activeIndex, pageIndex) {

            this.activeIndex = activeIndex;
            this.pageInfo();

            this.isDisablePreviou();
            this.isDisableNext();

            this.reqData.PageIndex = pageIndex;
            this.loadData.emit(this.reqData);
      }

      //显示tables数据信息
      private pageInfo() {
            let pageSize = this.reqData.PageSize;
            this.startItem = this.activeIndex * pageSize + 1;
            this.endItem = this.activeIndex * pageSize + pageSize;
            if (this.totalRows < this.endItem) {
                  this.endItem = this.totalRows;
            }
            if (this.reqData.PageSize > this.totalRows) {
                  this.endItem = this.totalRows;
            }
      }

      private isDisablePreviou() {
            if (this.activeIndex == 0) {
                  this.previouDisable = true;
            } else {
                  this.previouDisable = false;
            }
      }

      private isDisableNext() {

            if (this.activeIndex == 7 || this.activeIndex == Math.ceil(this.totalRows / this.reqData.PageSize) - 1) {
                  this.nextDisable = true;
            } else {
                  this.nextDisable = false;
            }
      }


}
